#include <OsgClassDataSource/Trajectory3DOsg.h>
#include <iostream>
#include <osgDB/ReadFile>
#include <FunctionsPluginOsg.h>
#include <ObjectInspectorDirectorOsg.h>
#include <EDesignerCore.h>
#include <Trajectory3D.h>
#include <Point3D.h>
#include <Functions.h>
#include <EObjectEvent.h>

Trajectory3DOsg::Trajectory3DOsg( QObject *parent /*= 0*/ ) : MatrixTransformOsg(parent)
{
    m_object = new Trajectory3D();
    this->refreshInfoContainer();
}


Trajectory3DOsg::~Trajectory3DOsg()
{

}

QColor Trajectory3DOsg::getLineColor()
{
    QColor result;
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        osg::Vec4 lineColor = trajectory->getLineColor();
        result.setRedF(lineColor[0]);
        result.setGreenF(lineColor[1]);
        result.setBlueF(lineColor[2]);
        result.setAlphaF(lineColor[3]);
    }
    return result;
}

void Trajectory3DOsg::setLineColor(const QColor &lineColor)
{
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        osg::Vec4 osgColor;
        osgColor[0] = lineColor.redF();
        osgColor[1] = lineColor.greenF();
        osgColor[2] = lineColor.blueF();
        osgColor[3] = lineColor.alphaF();
        trajectory->setLineColor(osgColor);
    }

}

double Trajectory3DOsg::getLineWidth()
{
    double lineWidth = 0;
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        lineWidth = trajectory->getLineWidth();
    }
    return lineWidth;
}


void Trajectory3DOsg::setLineWidth(double lineWidth)
{
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        trajectory->setLineWidth(lineWidth);
    }
}

void Trajectory3DOsg::setMTName(const QString &mtName)
{
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        trajectory->setMTName(mtName.toStdString());
    }

}

QString Trajectory3DOsg::getMTName()
{
    QString str;
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        str = QString::fromStdString( trajectory->getMTName() );
    }
    return str;
}

void Trajectory3DOsg::setLoopMode(LoopModeEnum loopMode)
{
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        trajectory->setLoopMode((osg::AnimationPath::LoopMode)loopMode);
    }

}

Trajectory3DOsg::LoopModeEnum Trajectory3DOsg::getLoopMode()
{
    osg::AnimationPath::LoopMode loopMode = (osg::AnimationPath::LoopMode)0;
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        loopMode = trajectory->getLoopMode();
    }
    return (Trajectory3DOsg::LoopModeEnum)loopMode;
}

void Trajectory3DOsg::setDurationTime(double durationTime)
{
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        trajectory->setDurationTime(durationTime);
    }
}

double Trajectory3DOsg::getDurationTime()
{
    double time = -1;
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        time = trajectory->getDurationTime();
    }
    return time;
}

QVarintTrigger Trajectory3DOsg::getClearPoints()
{
    return QVarintTrigger();
}

void Trajectory3DOsg::setClearPoints(QVarintTrigger trigger)
{
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        trajectory->removeChildren(0, trajectory->getNumChildren());

        osg::ref_ptr<Point3D> p1 = new Point3D;
        p1->setMatrix(osg::Matrix::translate(0,0,0));

        osg::ref_ptr<Point3D> p2 = new Point3D;
        p2->setMatrix(osg::Matrix::translate(0,0,5));

        trajectory->addChild(p1);
        trajectory->addChild(p2);
    }

}

bool Trajectory3DOsg::getAnimatEnable()
{
    bool boolValue = true;
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        boolValue = trajectory->getAnimatEnable();
    }
    return boolValue;
}

void Trajectory3DOsg::setAnimatEnable(bool boolValue)
{

    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        trajectory->setAnimatEnable(boolValue);
    }
}

double Trajectory3DOsg::getTimeOffset()
{
    double timeOffset = 0.0;
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        timeOffset = trajectory->getTimeOffset();
    }
    return timeOffset;
}

void Trajectory3DOsg::setTimeOffset(double doubleValue)
{
    Trajectory3D* trajectory = dynamic_cast<Trajectory3D*>(m_object.get());
    if(trajectory != NULL)
    {
        trajectory->setTimeOffset(doubleValue);
    }
}

bool Trajectory3DOsg::event(QEvent *e)
{
    bool result = false;
    if(e->type() == EEditorFunctions::EObjectEventType())
    {
        //
        EObjectEvent* eObjEvent = dynamic_cast<EObjectEvent*>(e);
        if(eObjEvent != NULL)
        {
            EObject* pointEObj = eObjEvent->getEObjectPtr();
            if(pointEObj != NULL && eObjEvent->getEventInfo() == "Point3DOsg::slotsMatrixChanged")
            {
                osg::Node* pointNodePtr = FunctionsPluginOsg::EObjectPtrToOsgNodePtr(pointEObj);
                Point3D* point = dynamic_cast<Point3D*>(pointNodePtr);
                if(point != NULL)
                {
                   Trajectory3D* trajectoryPtr = dynamic_cast<Trajectory3D*>(m_object.get());
                   if(trajectoryPtr != NULL)
                   {
                       trajectoryPtr->updatePointPosition(point);
                   }
                }
                result = true;
            }
        }
    }
    std::cout<<"Trajectory3DOsg::event"<<std::endl;
    return result;
}
